<?php
require_once "../config.php";
require_once SESSION_INCLUDE;
require_once DB;
require_once GALLERY;
require_once PHOTO;

session_start();

	$db = get_session_db($db_config);
	
	if(!user_is_logged($db))
	{
		header("Location:http://".LOGIN_FILE);
		exit();
	}
	
	$user = get_session_user();
	
	if(!isset($_POST['json']))
		exit();
		
	$action = json_decode(stripslashes($_POST['json']), true);
	$is_delete = true;
	
	//var_dump($action);
	
	$gallery_id = null;
	if(isset($action['gallery_id']))
	{
		$gallery_id =$action['gallery_id'];
		$is_delete = false;
	}
	
	$items = $action['items'];
	
	for($i=0; $i<count($items); $i++)
	{
		$photo = Photo::getPhotoFromId($db, $items[$i]);
		
		if($is_delete)
		{
			unlink(PHOTO_DIR . $photo->getFilename());
			unlink(PHOTO_DIR . $photo->getHash() . ".thumb.png");
			$photo->delete();
		}
		else
		{
			$photo->set('gallery_id', $gallery_id);
			$photo->commit();
		}
	}
	
	$gallery = Gallery::getGalleryFromId($db, $gallery_id);
	$gallery->set('gallery_modified', time());
	$gallery->commit();
	
?>
